package cc.booku.owlbox.UserSettingsPage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.R;
import cc.booku.owlbox.VideoPlayer.PlayerActivity;
import cc.booku.owlbox.VideoPlayer.VideoPlayerActivity;
import cc.booku.owlbox.loadSqlite.DatabaseHelper;
import cc.booku.owlbox.loadSqlite.collectDate;

public class UserDataActivity extends AppCompatActivity {

    private Toolbar Toolbar;
    private List<collectDate> collectDateList = new ArrayList<collectDate>();
    private DatabaseHelper helper;
    private SQLiteDatabase db;
    private String sqlSentence = "";
    private String sqlTitle = "";
    private NoScrollGridView userdata_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data);


        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        userdata_list = (NoScrollGridView) findViewById(R.id.userdata_list);


        initsqlite();
        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        sqlSentence = bundle.getString("sqlSentence");
        sqlTitle = bundle.getString("sqlTitle");
        LoadToolbar(sqlTitle);


//        sToolbar = (Toolbar)findViewById(R.id.toolbar);
//        setSupportActionBar(sToolbar);//利用Toolbar代替ActionBar
//        sToolbar.setTitle(sqlTitle);
//        sToolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });

        collectFunction(sqlSentence);
    }


    private void LoadToolbar(String sqlTitle) {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);

        Toolbar.setTitle(sqlTitle);

        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });

    }


    private void initsqlite() {

        //5.实例化SQLiteOpenHelper的子类对象-MyDataBaseHelper
        helper = new DatabaseHelper(this, "dbdate", null, R.string.db_version);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
    }

    private collectDate collectDateC;

    private String collectFunction(String SQLs) {

        // String vod_score, vod_name, vod_class, vod_pic, vod_play_url, vod_add_time,vod_value_add;
        collectDateList.clear();
        //数据库
//https://blog.csdn.net/weixin_39886154/article/details/80894089
        SQLiteDatabase db = helper.getWritableDatabase();
        //去除重复
        Cursor cursor = db.query(true, SQLs, new String[]{"vod_score", "vod_name", "vod_class", "vod_pic", "vod_play_url", "vod_add_time", "vod_value_add"}, null, null, "vod_name", null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                String vod_score = cursor.getString(cursor.getColumnIndex("vod_score"));
                String vod_name = cursor.getString(cursor.getColumnIndex("vod_name"));
                String vod_class = cursor.getString(cursor.getColumnIndex("vod_class"));
                String vod_pic = cursor.getString(cursor.getColumnIndex("vod_pic"));
                String vod_play_url = cursor.getString(cursor.getColumnIndex("vod_play_url"));
                String vod_add_time = cursor.getString(cursor.getColumnIndex("vod_add_time"));
                String vod_value_add = cursor.getString(cursor.getColumnIndex("vod_value_add"));

//                Log.d("Message", "title" + vod_score);
//                Log.d("Message", "imgurl" + vod_play_url);
//                Log.d("Message", "videourl" + vod_add_time);
//                Log.d("Message", "type" + vod_value_add);

                collectDateC = new collectDate();
                collectDateC.setVod_score(vod_score);
                collectDateC.setVod_name(vod_name);
                collectDateC.setVod_class(vod_class);
                collectDateC.setVod_add_time(vod_add_time);
                collectDateC.setVod_pic(vod_pic);
                collectDateC.setVod_play_url(vod_play_url);
                collectDateC.setVod_value_add(vod_value_add);


                collectDateList.add(collectDateC);
            } while (cursor.moveToNext());

            Message msg = new Message();
            msg.what = 16;
            handler.sendMessage(msg);//用activity中的handler发送消息
        }
        cursor.close();


        return null;
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {


                case 16:
                    USERcollectDate((ArrayList<collectDate>) collectDateList);
                    break;


            }
        }
    };


    private UserDataActivity.video_public_Adapter video_public_AdapterA;

    public void USERcollectDate(ArrayList<collectDate> collectDateList) {


        video_public_AdapterA = new UserDataActivity.video_public_Adapter(R.layout.customize_item, this, collectDateList);

        userdata_list.setAdapter(video_public_AdapterA);
        userdata_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //跳转提示
                // Toast.makeText(UserDataActivity.this, "Click item" + i, Toast.LENGTH_SHORT).show();
                final TextView vod_class1 = (TextView) view.findViewById(R.id.vod_class);
                final TextView vod_score1 = (TextView) view.findViewById(R.id.vod_score);
                final TextView vod_name1 = (TextView) view.findViewById(R.id.vod_name);
                final TextView vod_add_time1 = (TextView) view.findViewById(R.id.vod_add_time);
                final TextView vod_play_url1 = (TextView) view.findViewById(R.id.vod_play_url);
                final TextView vod_value_add1 = (TextView) view.findViewById(R.id.vod_value_add);


                String vod_class2 = vod_class1.getText().toString();
                String vod_score2 = vod_score1.getText().toString();
                String vod_name2 = vod_name1.getText().toString();
                String vod_add_time2 = vod_add_time1.getText().toString();
                String vod_play_url2 = vod_play_url1.getText().toString();
                String vod_value_add2 = vod_value_add1.getText().toString();


                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("vod_class", vod_class2);
                bundle.putString("vod_score", vod_score2);
                bundle.putString("vod_name", vod_name2);
                bundle.putString("vod_add_time", vod_add_time2);
                bundle.putString("vod_play_url", vod_play_url2);
                bundle.putString("vod_value_add", vod_value_add2);
                intent.putExtras(bundle);
                intent.setClass(UserDataActivity.this, PlayerActivity.class);
                startActivity(intent);

            }
        });

        userdata_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, final View view,
                                           final int i, long l) {

                final TextView vod_name1 = (TextView) view.findViewById(R.id.vod_name);
                String vod_name2 = vod_name1.getText().toString();


                Bundle bundle = getIntent().getExtras();
                sqlTitle = bundle.getString("sqlTitle");
                sqlSentence = bundle.getString("sqlSentence");
                db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
                if (sqlTitle.equals("我的收藏")) {

                    db.delete("loveTable", "vod_name=?", new String[]{vod_name2});
                    db.close();
                    Toast.makeText(UserDataActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                    //collectFunction(sqlSentence);
                } else if (sqlTitle.equals("下载内容")) {
                    db.delete("downTable", "vod_name=?", new String[]{vod_name2});
                    db.close();
                    Toast.makeText(UserDataActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                } else if (sqlTitle.equals("历史记录")) {
                    db.delete("collectTable", "vod_name=?", new String[]{vod_name2});
                    db.close();
                    Toast.makeText(UserDataActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(UserDataActivity.this, "删除失败", Toast.LENGTH_SHORT).show();

                }
                //collectDateList.clear();

                //db.close();

                return true;     // 这里一定要改为true，代表长按自己消费掉了，若为false，触发长按事件的同时，还会触发点击事件</span></strong>
            }
        });


    }

    class video_public_Adapter extends BaseAdapter implements ListAdapter {


        private ArrayList<collectDate> collectDateList;
        private int id;
        private Context context;
        private LayoutInflater inflater;

        public video_public_Adapter(int item, Context context, ArrayList<collectDate> collectDateList) {
            this.collectDateList = collectDateList;
            this.context = context;
            this.id = item;
            inflater = LayoutInflater.from(context);

        }

        @Override
        public int getCount() {
            return collectDateList.size();
        }

        @Override
        public Object getItem(int i) {
            return collectDateList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @SuppressLint("WrongConstant")
        @Override

        public View getView(int i, View view, ViewGroup viewGroup) {


            TextView vod_score = null;
            TextView vod_name = null;
            TextView vod_class = null;
            TextView vod_add_time = null;
            ImageView vod_pic = null;
            TextView vod_play_url = null;
            TextView vod_value_add = null;

            UserDataActivity.video_public_Adapter.ViewHolder viewHolder;
            if (view == null) {
                view = inflater.inflate(id, null);

                vod_score = (TextView) view.findViewById(R.id.vod_score);
                vod_name = (TextView) view.findViewById(R.id.vod_name);
                vod_class = (TextView) view.findViewById(R.id.vod_class);
                vod_add_time = (TextView) view.findViewById(R.id.vod_add_time);
                vod_value_add = (TextView) view.findViewById(R.id.vod_value_add);
                vod_play_url = (TextView) view.findViewById(R.id.vod_play_url);

                vod_pic = (ImageView) view.findViewById(R.id.vod_pic);


                view.setTag(new UserDataActivity.video_public_Adapter.ViewHolder(vod_score, vod_name, vod_class, vod_add_time, vod_value_add, vod_play_url, vod_pic));
            } else {
                UserDataActivity.video_public_Adapter.ViewHolder viewHolder1 = (UserDataActivity.video_public_Adapter.ViewHolder) view.getTag(); // 重新获取ViewHolder
                vod_score = viewHolder1.vod_score;
                vod_class = viewHolder1.vod_class;
                vod_name = viewHolder1.vod_name;
                vod_add_time = viewHolder1.vod_add_time;
                vod_value_add = viewHolder1.vod_value_add;
                vod_play_url = viewHolder1.vod_play_url;


            }
            collectDate video_public_classListc = (collectDate) collectDateList.get(i); // 获取当前项的实例

            //video_public_classListc
            vod_name.setText(video_public_classListc.getVod_name().toString());//对象为空
            vod_class.setText(video_public_classListc.getVod_class().toString());
            vod_score.setText(video_public_classListc.getVod_score().toString());
            vod_add_time.setText(video_public_classListc.getVod_add_time().toString());
            vod_value_add.setText(video_public_classListc.getVod_value_add().toString());
            vod_play_url.setText(video_public_classListc.getVod_play_url().toString());
            LoadImage(vod_pic, video_public_classListc.getVod_pic());

            return view;

        }

        private void LoadImage(ImageView img, String path) {
            //ImageLoader.loadImage(image_url, filmclass.getPimg());
            //Log.d("LoadImage", "img=====" + path);
            AsyncTaskImageLoad async = new AsyncTaskImageLoad(img);
            async.executeOnExecutor(Executors.newCachedThreadPool(), path);//快的多
            //async.execute(path);
        }

        private final class ViewHolder {
            TextView vod_score = null;
            TextView vod_name = null;
            TextView vod_class = null;
            TextView vod_add_time = null;
            ImageView vod_pic = null;
            TextView vod_play_url = null;
            TextView vod_value_add = null;


            public ViewHolder(TextView vod_score, TextView vod_name, TextView vod_class, TextView vod_add_time, TextView vod_value_add, TextView vod_play_url, ImageView vod_pic) {
                this.vod_class = vod_class;
                this.vod_score = vod_score;
                this.vod_name = vod_name;
                this.vod_add_time = vod_add_time;
                this.vod_play_url = vod_play_url;
                this.vod_value_add = vod_value_add;
                this.vod_pic = vod_pic;
            }

        }
    }

}
